<?php

use Doraemon\service\Format\Note\HandleNote;
use Doraemon\tools\HhzNiceLivingUrl;
use Doraemon\service\Format\Guide\HandleGuide;

/**
 * 居住任意门
 */
class AnyDoorAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    const  ALL_DATA = [
        1 => [
            'tab_id' => 1,
            'tab_name' => '西门子',
            'tab_desc' => '山系平行生活新构想',
            'cover_img' => 'https://img.haohaozhu.cn/Op-imageShow/0338730h00i20tA00ry4t7h?iv=1',
            'url' => 'https://lego.haohaozhu.cn/lego/636.html?preview=1',//任意门链接
        ],
        2 => [
            'tab_id' => 2,
            'tab_name' => '三星',
            'tab_desc' => '艺术大师之家',
            'cover_img' => 'https://img.haohaozhu.cn/Op-imageShow/c7fe430h00i20YZ00rya3ig?iv=1',
            'home_information' => ['00001t702000pkrt'],//家居资讯
            'url' => 'https://lego.haohaozhu.cn/lego/654.html?preview=1',//任意门链接
        ],
        3 => [
            'tab_id' => 3,
            'tab_name' => '华帝',
            'tab_desc' => '帝造乡村理想乡',
            'cover_img' => 'https://img.haohaozhu.cn/Op-imageShow/cb48730h00i20fM00ry4t73?iv=1',
            'url' => 'https://lego.haohaozhu.cn/lego/599.html',//任意门链接
        ],
        4 => [
            'tab_id' => 4,
            'tab_name' => '三翼鸟',
            'tab_desc' => '智慧生活样板间',
            'cover_img' => 'https://img.haohaozhu.cn/Op-imageShow/2067930h20i20Ch00ry4t7s?iv=1',
            'daren_dynamic' => ['0066pgl00000pkrt', '00657mz00000pkrt'],
        ],
        5 => [
            'tab_id' => 5,
            'tab_name' => '京东京造',
            'tab_desc' => '好好住进温暖家',
            'cover_img' => 'https://img.haohaozhu.cn/Op-imageShow/64a9930h00i202J00rya3je?iv=1',
            'url' => 'https://lego.haohaozhu.cn/lego/563.html?preview=1',//任意门链接
            'daren_dynamic' => ['00616so00000a15g', '00616id000002quz', '00616i60000083uk', '00616i60000083uk', '00616j7000001d7o', '00616m4000001o1m'],//达人动态
            'hot_topic' => [//热门话题
                [
                    'topic_id' => 1685,
                    'topic_name' => '好好住进温暖家',
                ]
            ],
            'home_information' => ['00001s402000pkrt'],//家居资讯
        ],
        6 => [
            'tab_id' => 6,
            'tab_name' => '立邦',
            'tab_desc' => '艺术与远方不远',
            'cover_img' => 'https://img.haohaozhu.cn/Op-imageShow/2067930h20i20Ch00ry4t7s?iv=1',
            'daren_dynamic' => ['0062hy400000pkrt', '0061y8k00000pkrt', '00631n200000pkrt'],//达人动态
        ],
    ];

    public function main()
    {
        $tabId = $this->getRequest()->getQuery('tab_id', 0);
        if (0 == $tabId) {
            $tabId = rand(1, count(self::ALL_DATA));
        }
        $tabData = self::ALL_DATA[$tabId] ?? null;
        $data = [
            'tabs' => [],
            'tab_info' => null,
            'daren_dynamic' => [],//达人动态
            'hot_topic' => [],//热门话题
            'home_information' => [],//家居资讯
        ];
        foreach (self::ALL_DATA as $row) {
            $link = $row['url'] ? $this->getLink($row['url']) : '';
            $tabInfo = [
                'tab_id' => $row['tab_id'],
                'tab_name' => $row['tab_name'],
                'tab_desc' => $row['tab_desc'],
                'cover_img' => $row['cover_img'],
                'link' => $link,
            ];
            $data['tabs'][] = $tabInfo;
            if ($tabId == $row['tab_id']) {
                $data['tab_info'] = $tabInfo;
            }
        }
        if ($tabData) {
            if ($tabData['daren_dynamic']) {
                $data['daren_dynamic'] = $this->getDarenDynamic($tabData['daren_dynamic']);
            }
            if ($tabData['hot_topic']) {
                $data['hot_topic'] = $tabData['hot_topic'];
            }
            if ($tabData['home_information']) {
                $data['home_information'] = $this->getHomeInformation($tabData['home_information']);
            }
        }
        if ($data['tab_info'] && empty($data['tab_info']['link'])) {
            $tempKeys = ['daren_dynamic', 'hot_topic', 'home_information'];
            shuffle($tempKeys);
            foreach ($tempKeys as $tempKey) {
                if ($tabData[$tempKey] && !$data['tab_info']['link']) {
                    switch ($tempKey) {
                        case 'daren_dynamic':
                            $index = array_rand($tabData['daren_dynamic']);
                            $noteId = $tabData['daren_dynamic'][$index];
                            $data['tab_info']['link'] = HhzNiceLivingUrl::getNoteDetailLink($noteId);
                            break;
                        case 'hot_topic':
                            $index = array_rand($tabData['hot_topic']);
                            $topicInfo = $tabData['hot_topic'][$index];
                            $linkData = [
                                "topic_id" => $topicInfo['topic_id'],
                                "keyword" => $topicInfo['topic_name'],
                            ];
                            $data['tab_info']['link'] = HhzNiceLivingUrl::getTopicDetailLink($linkData);
                            break;
                        case 'home_information':
                            $index = array_rand($tabData['home_information']);
                            $guidId = $tabData['home_information'][$index];
                            $data['tab_info']['link'] = HhzNiceLivingUrl::getGuideDetailLink($guidId);
                            break;
                    }
                }
            }
        }
        $this->setSuccess($data);
    }

    private function getLink($url, $isFullScreen = false)
    {
        if ($isFullScreen) {
            $link = 'niceliving://webview/fullscreen?protego=' . urlencode(base64_encode(json_encode(['url' => $url])));
        } else {
            $link = 'niceliving://webview/normalscreen?protego=' . urlencode(base64_encode(json_encode(['url' => $url])));
        }
        return $link;
    }

    private function getDarenDynamic($objIds)
    {
        $res = HandleNote::handleNote($objIds, HandleNote::MASONRY_LAYOUTS, $this->uid);
        return array_values($res);
    }

    private function getHomeInformation($objIds)
    {
        $res = HandleGuide::handleGuide($objIds, HandleGuide::MASONRY_LAYOUTS, $this->uid);
        return array_values($res);
    }

}